﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using EShop.Common;
using EShop.BussinessObjects;

namespace EShop.DataAccess
{
    public class CDAIngredients
    {

        public static List<Ingredient> Select(Ingredient ingredient)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            con.Open();


            string _cmdText =
                 "SELECT i.* " +
                 "FROM ingredient i " +
                 "WHERE ((i.ID = " + ingredient.ID.ToString() + ") OR (" + ingredient.ID.ToString() + " = " + ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective).ToString() + "))" +
                 "ORDER BY i.ID";

            SqlCommand cmd = new SqlCommand(_cmdText, con);
            SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
            DataTable tbl = new DataTable();
            dataAdapter.Fill(tbl);

            List<Ingredient> _ingredientList = new List<Ingredient>();

            foreach (DataRow row in tbl.Rows)
            {
                Ingredient _ingredient = new Ingredient();
                _ingredient.ID = (row["ID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ID"]));
                _ingredient.Name = (row["Name"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue).ToString() : Convert.ToString(row["Name"]));

                _ingredientList.Add(_ingredient);
            }

            con.Close();
            return _ingredientList;
        }
    }
}